import javax.crypto.Cipher;
import javax.swing.*;
import java.awt.*;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class RSADecryptTool extends JFrame {
    private JTextArea inputArea;
    private JTextArea outputArea;
    private JButton decryptButton;

    // RSA私钥 - 实际使用时替换成你的私钥
    private static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCAXX1WogoPv44U9y4tULfwqmcfKepzdN9jxgxyDrC5fwgXQKYquJEfdXo2hU1B1Yl/JGWZgxM+d6NibVtIoa5Z7c9/HlHKGmQUXjU5ijZQHG8Ed+a3wLbnmSeUpvi/veK3oYy5RSC3VozxvlfsGS/5FmCcNTQig6tVdmln6oTtewP3KYxrMYqWOh4jXCBwj5/NcY/+9XRfnL++xEbFqWYN7hWfDCyGz4ao648kpB45y03+9ZRzS5hjZYF4I4t3t2C9a5Q9wHYC7QU3twGX+/8AfKxj/oeQu/x8I87PfohEVfy93Yyf44xUd02K3SbCgdQhL0jFBWRiX4FhRu7vwyHHAgMBAAECggEAX1aQd96jwGySN2oaptdztlCphJDaK+Ra4jy/E5trkGoT9Ukz4TjxuD7FhWbeKY5Sr9a1ix26FkQ/WiREjx5eYzZoTU6UipmtSR9rQpAvpLJplhUDi+ibMEolBpg42hhLIf3fid8Y3HJw/j9UB+vxIHyJRpuRXC0U/MWUpfSdw87rE+GQrZyKyTgxINrBsabHz8ycUjrNXksQLuYNwyu3Ih+IhsOvuspEjEvyCnpmyNNq/8VBsuqrzyUfaq+cFYtk1x8GsYSV06h01UWxIZLleVZfOfaOYaMizMqmFsZJT5zmT4Gqqfpc3gFyFja2ZJFAR+bAhzo5iEDhwFI/SkurYQKBgQDpPblUAW1ky9yjvTYAypioFkRVdih+cy32IRxKoLYqLMSKEoOGNgV2R6fnh+8mgc68xw5uRv5MsAJcXFAd+DqXQFk/bzTzAU0M94liBw6O7q91vXuh/+9OgA15rDMd9L9J9iz4o8FLr6LRpEkb4Zh5i/c2bvEYPQSfPV2myGPesQKBgQCM5AEc+APrslsxl+B8lEvFyQZXF/cjPjrCY8EnH2ze//aCjyLUs2Rd8TSZwxECtE8pUC3JCK2iEjV5kJFsfh4UWmBQH4RN+Yzdd1Sb4hcKBqRcZ9zgpwB7bpeKTCGz5owfRgUgeaNZk51MAWA3/JqgiCDWtfmIpFh9harQJH7V9wKBgQDXyq2elTt62Eooi7RdAxQ932wVjZ5lb3cEkxmzygEAAC9Od1aMY4qmJiGoStHuVIbLjXdCfBhnJyjn/th5AeJjcbugvujEM13wM00+Dr1fbGGVk1vBRCNKA5t0COe6PWiFq2dsG0WJGtSPFWo2i4AgZGiySe5xuHKASaxN1HTh4QKBgBec/zLDvqR6OB6PvegjUq/LiWx/b8RQYbjCz1SnR6/4ZnBrXQMZLfEwxAghUXCKl9nsvj+7XJ6VPrEpMbZlTNVMcGf9YEdRU/KqVVvrAMGFa3axUau1UbdeSbe8Gsn0EfgL3EAdIuuYefyLHn1DsQbGyfoydagZI1umU4KfG0LxAoGAAlak5XtNp2IRNIZBPQWZkrNi5tH9Ssam7OEDTdFO+iFKwOtUNRiOw1ur19Sog5TGzm7NUqJOQB3/dRqGVe6SK5mttG00LQg9YLsoljlP3NEl8NWv09gIpmm6C4IzsI8faEQVen3GI0S3lz3NheGncLPZ/n23H6Y7d1i/6Z+IpQg=";

    public RSADecryptTool() {
        setTitle("RSA解密工具");
        setSize(1300, 900);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置全局字体
        Font largeFont = new Font("微软雅黑", Font.PLAIN, 26);
        UIManager.put("TextArea.font", largeFont);
        UIManager.put("Label.font", largeFont);
        UIManager.put("Button.font", new Font("微软雅黑", Font.BOLD, 28));

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建输入区域
        JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
        JLabel inputLabel = new JLabel("输入密文:");
        inputLabel.setFont(largeFont);
        inputPanel.add(inputLabel, BorderLayout.NORTH);

        inputArea = new JTextArea(8, 40);
        inputArea.setFont(largeFont);
        inputArea.setLineWrap(true);
        inputPanel.add(new JScrollPane(inputArea), BorderLayout.CENTER);

        // 创建输出区域
        JPanel outputPanel = new JPanel(new BorderLayout(5, 5));
        JLabel outputLabel = new JLabel("解密结果:");
        outputLabel.setFont(largeFont);
        outputPanel.add(outputLabel, BorderLayout.NORTH);

        outputArea = new JTextArea(8, 40);
        outputArea.setFont(largeFont);
        outputArea.setLineWrap(true);
        outputArea.setEditable(false);
        outputPanel.add(new JScrollPane(outputArea), BorderLayout.CENTER);

        // 创建按钮
        decryptButton = new JButton("解 密");
        decryptButton.setFont(new Font("微软雅黑", Font.BOLD, 28));
        decryptButton.setPreferredSize(new Dimension(400, 60));
        decryptButton.addActionListener(e -> performDecryption());

        // 将所有组件添加到主面板
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.add(inputPanel);
        centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        centerPanel.add(outputPanel);

        mainPanel.add(centerPanel, BorderLayout.CENTER);

        // 创建按钮面板使按钮居中
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(decryptButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    private void performDecryption() {
        try {
            String encryptedText = inputArea.getText();

            if (encryptedText.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请输入密文！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String decryptedText = decrypt(encryptedText);

            if (decryptedText != null) {
                outputArea.setText(decryptedText);
            } else {
                outputArea.setText("解密失败！");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "解密过程出错：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private static String decrypt(String content) {
        try {
            byte[] inputByte = Base64.getDecoder().decode(content);
            byte[] decoded = Base64.getDecoder().decode(PRIVATE_KEY);
            PrivateKey priKey = KeyFactory.getInstance("RSA")
                    .generatePrivate(new PKCS8EncodedKeySpec(decoded));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, priKey);
            byte[] result = cipher.doFinal(inputByte);
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            RSADecryptTool tool = new RSADecryptTool();
            tool.setVisible(true);
        });
    }
}

/*
要将此程序打包成可执行文件，请按照以下步骤操作：

1. 首先确保代码能正常编译运行

2. 创建MANIFEST.MF文件
在项目根目录下创建 META-INF/MANIFEST.MF 文件，内容如下：
Manifest-Version: 1.0
Main-Class: RSADecryptTool

3. 使用命令行打包：
javac RSADecryptTool.java
jar cvfm RSADecryptTool.jar META-INF/MANIFEST.MF *.class

4. 使用Maven打包（推荐）：
创建pom.xml文件：

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>rsa-decrypt-tool</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>RSADecryptTool</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

然后执行: mvn clean package

5. 将Java程序转换为本地可执行文件（可选）：
可以使用Launch4j或JPackage工具将JAR文件转换为.exe文件（Windows）或.app文件（Mac）

6. 运行：
- JAR文件运行方式：java -jar RSADecryptTool.jar
- 如果转换成了exe，直接双击运行即可
*/



